package com.example.shopuser.service.impl;

import com.example.shopcommon.dao.entity.User;
import com.example.shopuser.dao.UserDao;
import com.example.shopuser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User get(Long id) {
        return userDao.findById(id).get();
    }

    @Override
    public void reduct(User user) {
        User one = userDao.findById(user.getId()).get();
        if(one.getMoney().compareTo(user.getMoney())<0){
            throw new RuntimeException("Not Enough Money ...");
        }
        one.setMoney(one.getMoney().subtract(user.getMoney()));
        userDao.save(one);
    }
}
